 //
//  ThumbImageView.m
//  WeiboSpace
//
//  Created by nan4 li on 12-4-24.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "ThumbImageView.h"
#import "ImageDLoader.h"

@interface ThumbImageView()
-(void)removeSelfFromSuperView;
@end

@implementation ThumbImageView

- (id)initWithFrame:(CGRect)frame withimageurl:(NSString *)imageurl
{
    self = [super initWithFrame:frame];
    if (self) {
        
        //半透明背景
        CGRect rect = self.frame;
        //rect.origin = CGPointMake(0, 0);
        
        UIView * backgroundView = [[UIView alloc] initWithFrame:rect];
        backgroundView.backgroundColor = [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.75f];
        [self addSubview:backgroundView];
        [backgroundView release];
        
        //初始化
        rect = CGRectMake(100, 100, 120, 230);
        imageView = [[UIImageView alloc]initWithFrame:rect];
        
        //下载图片
        //ImageDLoader *imageDown = [[ImageDLoader alloc]init];
        //[imageDown getImageFromURL:imageurl UiImage:nil delegate:self];
        //[imageDown release];
        //[[XImageLoader shareXImageLoader] DownLoadImage:imageurl delegate:self];
        [self addSubview:imageView];
        
        ImageDLoader *downloader = [[ImageDLoader alloc] init];
        [downloader getImageFromURL:imageurl delegate:self];
        [downloader release];
        
    }
    return self;
}

//试图的淡入
- (void)fadein
{
    CGContextRef context = UIGraphicsGetCurrentContext();
    [UIView beginAnimations:nil context:context];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseIn];
    [UIView setAnimationDuration:0.5f];
    [self setAlpha:1.0f];
    [UIView commitAnimations];
}

-(void)dealloc
{
    [imageView release];imageView = nil;
    [super dealloc];
}

#pragma overwright UIResponder
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch * touch = [touches anyObject];
    
    CGPoint pt = [touch locationInView:self];
    if (!CGRectContainsPoint(imageView.frame, pt)) 
    {
        [self fadeout];
    }
}

// 视图的淡出
- (void)fadeout
{
    CGContextRef context = UIGraphicsGetCurrentContext();
    [UIView beginAnimations:nil context:context];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseIn];
    [UIView setAnimationDuration:0.5f];
    [self setAlpha:0.0f];
    [UIView commitAnimations];
    [self performSelector:@selector(removeSelfFromSuperView) withObject:nil afterDelay:1.0];
}

//自己从父视图上移除；
- (void)removeSelfFromSuperView
{
    [self removeFromSuperview];
}

#pragma mark XImageLoaderDelegate
//- (void)ima:(NSData*)imageData sender:(ImageDLoader*)sender
//- (void)ImageLoaded:(NSData*)imageData sender:(XImageLoader*)sender
- (void)receivedImage:(NSData*)imageData sender:(ImageDLoader*)sender
{
	
	if (imageData==nil||[imageData length]==0) {
		return;
	}
	
	UIImage *imageCtl = [UIImage imageWithData:imageData];
	[imageView setImage:imageCtl];
    
    CGSize imageSize =    imageCtl.size;
        
    CGFloat oldWidth =  imageSize.width;
    CGFloat oldHeight = imageSize.height;
    
    CGFloat baseWidth = 220;
    CGFloat baseHeight = 330;
    
    CGFloat newWidth = 0.0f;
    CGFloat newHeight = 0.0f;
    
    
    if (oldWidth/oldHeight>baseWidth/baseHeight) 
    {
        newWidth = 220;
        CGFloat xRadio = newWidth/oldWidth;
        newHeight = oldHeight*xRadio;
    }
    
    else
    {
        newHeight = 330;
        CGFloat xRadio = newHeight/oldHeight;
        newWidth = oldWidth*xRadio;
        
    }
    
    //重新设置
    imageView.frame = CGRectMake((320-newWidth)/2, (480-newHeight)/2-30, newWidth, newHeight);
    //imageView.bounds = CGRectMake(50,50, newWidth,newHeight);
    
    
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/

@end
